//
//  WBHomeTitleBtn.swift
//  Swift_sina
//
//  Created by TFB_WGP on 2017/9/13.
//  Copyright © 2017年 TFB_WGP. All rights reserved.
//

import UIKit

class WBHomeTitleBtn: UIButton {

    /// 重载构造函数
    /// 如果title为nil就显示首页,否则显示title和箭头
    init(title: String?) {
        super.init(frame: CGRect())
        
        //判断title
        if title == nil {
            setTitle("首页", for: [])
        }else {
            setTitle(title! + " ", for: [])
        }
        
        titleLabel?.font = UIFont.systemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: [])
        
        setImage(UIImage(named:"navigationbar_arrow_down"), for: [])
        setImage(UIImage(named:"navigationbar_arrow_up"), for: .selected)
        
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let label = titleLabel, let iconView = imageView else {
            return
        }
        
        var labelFrame = label.frame
        labelFrame.origin.x = 0
        label.frame = labelFrame
        
        var iconViewFrame = iconView.frame
        iconViewFrame.origin.x = labelFrame.size.width
        iconView.frame = iconViewFrame
        
    }
}
